<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Address extends MY_Controller {
	
    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->data['controller'] = 'address';
        $this->load->model('Maddress');
    }
    
    
    public function index()
	{
	    $this->checkAdminSystemGroup();
        // bd phan trang cho list.
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 50;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,1);
        $offset = (($this->getParamUri(2,1) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,1) - 1) * $config['per_page']) : 0;
        $data = $this->Maddress->getListAddress($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,1);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        // h phan trang cho list.
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('province', 'Tỉnh/thành phố', 'required');
        $this->form_validation->set_rules('district', 'Quận/huyện', 'required');
        $this->form_validation->set_rules('address', 'Địa chỉ', 'required');
        $this->form_validation->set_rules('name', 'Tên cửa hàng', 'required');
        $this->form_validation->set_rules('group', 'Nhóm mạng', 'required');
        if($this->form_validation->run() === TRUE)
        {
            $type = $this->getParamString('type');
            $arr = array(
                'id_province'   => $this->getParamInt('province'),
                'id_district'   => $this->getParamInt('district'),
                'address'       => $this->getParamString('address'),
                'lat'           => $this->getParamString('lat'),
                'long'          => $this->getParamString('long'),
                'name'          => $this->getParamString('name'),
                'group_id'      => $this->getParamInt('group')
            );
            if($type === 'add'){        
                $f = $this->Maddress->addAddress($arr);
                
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Thêm thành công!");window.location="'.$this->data['baseurl'].'address";</script>');
                }
            }elseif($type === 'edit'){
                $id = $this->getParamInt('id');
                $f = $this->Maddress->editAddress($id,$arr);
                
                if($f == '200'){
                    die('<meta charset="utf-8"><script type="text/javascript">alert("Cập nhật thành công!");window.location="'.$this->data['baseurl'].'address";</script>');
                }
            }
            
        }
        
        $this->data['group'] = $this->Maddress->getGroup();
        $this->data['province'] = $this->getProvince();
	    $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/layout', $this->data);
	}
    
    public function addressdetail()
    {
        $this->checkAdminSystemGroup();
        $this->data['data'] = $this->Maddress->getAddressDetail($this->getParamInt('id'));
        $this->data['group'] = $this->Maddress->getGroup();
        $this->data['province'] = $this->getProvince();
        $this->data['district'] = $this->getDistrictData($this->data['data']->id_province);
        $this->data['action'] = __FUNCTION__;
		$this->load->view('temp/address/addressdetail', $this->data);
    }
    
    public function deleteaddress()
    {
        $this->checkAdminSystemGroup();
        $f = $this->Maddress->deleteAddress($this->getParamInt('id'));
        if($f == '200'){
            //die('<meta charset="utf-8"><script type="text/javascript">alert("Xóa thành công!");window.location="'.$this->data['baseurl'].'address";</script>');
            redirect('address');
        }
    }
	
    public function getDistrict()
    {
        die($this->data['fview']->selectOption($this->getDistrictData($this->getParamInt('id')), '', 'w99', 'district', 'district'));
    }
    
}
